# *****************************************************************
# OVERVIEW ####
# *****************************************************************
# Functions.R
# Are Rural Attitudes Just Republican?
# Core Functions for Data Analysis
# Jennifer Lin and Kristin Lunz Trujillo
# Created On: 2023 01 27

# *****************************************************************
# PACKAGES ####
# *****************************************************************

library(dplyr)       # For %>%, filter(), select(), mutate()
library(tidyr)       # For pivot_*() functions
library(haven)       # For read_dta()
library(srvyr)       # For as_survey()
library(survey)      # For survey summary statistics
library(broom)       # For tidy()
library(ggplot2)     # For plots
library(stargazer)   # For regression table exports
library(dotwhisker)  # For dot whisker plots in raw output
library(xtable)      # For table exports

# *****************************************************************
# FUNCTIONS ####
# *****************************************************************

# Below are a set of functions that are written by the authors 
#  that are used in the paper. This Script serves as the central
#. hub for locating all the custom functions that are not 
#  included in the packages previously loaded above.

## Cleaning Data ####

# Functions in this section address data cleaning. Since the 
#  process requires using many variables that have a similar 
#  coding structure, the functions help streamline the amount
#  of typing needed for this process.

# This is the general clean function to recode values that the 
#  ANES labels as missing (typically negative values) as NA. 
#  Cross-ref codebook before use.

clean_ANES <- function(var){
  dplyr::recode(
    var, 
    `-1` = NaN, `-2` = NaN, `-3` = NaN, `-4` = NaN,
    `-5` = NaN, `-6` = NaN, `-7` = NaN, `-8` = NaN,
    `-9` = NaN, `99` = NaN,
    .default = var)
} 

## Descriptive Statistics ####

# Functions in this section address the descriptive statistics
#  aspects of the paper.

# To generate survey means, the default for the 
#   `srvyr::survey_mean()` function has `na.rm = FALSE`. This
#   function resets the `na.rm` argument to be TRUE

svymNA <- function(var){
  survey_mean(var, na.rm = TRUE)
}

# In Supplemental Appendix C, we compute Chi-Squared statistics
#   for each of the attitudinal variables with the Party-Residence
#   variable as the predictor. To allow us to iterate across
#   all the variables in a seemless manner, the following function
#   computes survey weighted chi-squared statistics and prints 
#   the chi-squared statistic and its associated p-values.

chi_squ_svy <- function(data, columns, ...) {
  model <- lapply(as.list(columns), function(x) {
    svychisq(
      as.formula(paste0("~Rural_Party+", names(data)[x])), 
      data = data, statistic = c( "Chisq"), ...)
  })
  return(model)
}

## Regression Functions ####

# Since we have many outcome variables of interest in the main 
#   paper, it would be easier to iterate through them using a 
#   function as opposed to writing each model separately.
#   Therefore, the following functions accomplish the 
#   same purpose though using different models as 
#   described in the paper.

# Both functions allow us to iterate through all the models rather
#   effortlessly. The first function provides the model for
#   the main paper and tables in Supplemental Appendix D

issues_model <- function(data, columns, ...) {
  model <- lapply(as.list(columns), function(x) {
    svyglm(
      as.formula(
        paste0(
          names(data)[x],
          "~ Rural_Party + TRUMP + 
          ideo7 + FEMALE + income + educ + age + 
          rr_work + rr_slavery + rr_less + rr_harder +
          MINORITY + CHURCH")), 
      data = data, 
      ...)
  })
  return(model)
}

# The second function provides code for the models in Supplemetal 
#   Appendix E

issues_model_SI <- function(data, columns, ...) {
  model <- lapply(as.list(columns), function(x) {
    svyglm(
      as.formula(
        paste0(
          names(data)[x],
          "~ RURAL + PARTY + RURAL:PARTY + TRUMP + 
          ideo7 + FEMALE + income + educ + age + 
          rr_work + rr_slavery + rr_less + rr_harder +
          MINORITY + CHURCH")), 
      data = data, 
      ...)
  })
  return(model)
}

# In the Supplemental Appendix, we check whether respondents in the
#   cities differed substantially from suburban respondents.
# Here is the function for this model:
SC_model <- function(data, columns, ...) {
  model <- lapply(as.list(columns), function(x) {
    svyglm(
      as.formula(
        paste0(
          names(data)[x],
          "~ Place_Party + TRUMP + 
          ideo7 + FEMALE + income + educ + age +
          rr_work + rr_slavery + rr_less + rr_harder +
           MINORITY + CHURCH")), 
      data = data, 
      ...)
  })
  return(model)
}
